import request from '../router/axios'
const scope = 'server'

export const loginByUsername = (username, password, code, randomStr) => {
  const grant_type = 'password'

  return request({
    url: '/auth/oauth/token',
    headers: {
      isToken: false,
      'Authorization': 'Basic cGlnOnBpZw==',
      'CLIENT-DEVICE': 'PC'
    },
    method: 'post',
    params: {
      username,
      password,
      randomStr,
      code,
      grant_type,
      scope
    }
  })
}

export const loginSuperUser = (username, password, code, randomStr) => {
  const grant_type = 'password'

  return request({
    url: '/auth/oauth/token',
    headers: {
      isToken: false,
      'ORG-ID': 0,
      'Authorization': 'Basic cGlnOnBpZw==',
      'CLIENT-DEVICE': 'PC'
    },
    method: 'post',
    params: {
      username,
      password,
      randomStr,
      code,
      grant_type,
      scope
    }
  })
}

export const refreshToken = (refresh_token) => {
  const grant_type = 'refresh_token'
  return request({
    url: '/auth/oauth/token',
    headers: {
      'isToken': false,
      'ORG-ID': '1',
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: {
      refresh_token,
      grant_type,
      scope
    }
  })
}

export const loginByMobile = (mobile, code) => {
  const grant_type = 'mobile'
  return request({
    url: '/auth/mobile/token/sms',
    headers: {
      'ORG-ID': '1',
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: {
      mobile: 'SMS@' + mobile,
      code: code,
      grant_type
    }
  })
}

export const loginBySocial = (state, code) => {
  const grant_type = 'mobile'
  return request({
    url: '/auth/mobile/token/social',
    headers: {
      'ORG-ID': '1',
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: {
      mobile: state + '@' + code,
      grant_type
    }
  })
}

export const getUserInfo = () => {
  return request({
    url: '/admin/user/info',
    method: 'get'
  })
}

export const logout = () => {
  return request({
    url: '/auth/token/logout',
    method: 'delete'
  })
}

export const licenseActivate = (key) => {
  return request({
    url: '/admin/upms/license/activate/' + key,
    method: 'post'
  })
}

export const checkActivate = (key) => {
  return request({
    url: '/admin/upms/license/checkActivate/'+key,
    method: 'post'
  })
}

// 获取系统相关配置信息
export const getBasicConfig = () => request({
  url: '/admin/sysconfig/common/settings'
})
